
package view;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JPanel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import model.core.torneo.Gara;
import model.core.torneo.Torneo;
import model.core.torneo.calendario.Calendario;
import model.core.utente.Giocatore;
import model.core.utente.GiocatoreInterface;

/**
 * 
 * @author Giuseppe Fusco (fusgius@gmail.com)
 *
 */
public class CalendarioTorneo extends javax.swing.JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = -7768221410776817688L;

	private Torneo torneo;
	private Calendario cal;
	private int curgara, sizeCal;

	public CalendarioTorneo() {
		initComponents();
		addListener();
	}

	public void setTorneo(Torneo torneo) {
		this.torneo = torneo;
		fillGui();
	}

	private void fillGui(){
		if(torneo!=null){
			cal = torneo.getCalendario();
			if(cal!=null && cal.getSize()>0){
				sizeCal = cal.getSize();
				totaleGare.setText(String.valueOf(sizeCal));
				firstGara.doClick();
			}
		}
	}

	private void addListener(){
		nextGara.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				if(curgara<sizeCal-1){
					curgara++;
					nroGara.setText(String.valueOf(curgara+1));
					loadGaraIntoGui(cal.getAtPosition(curgara));
				}
			}
		});

		previousGara.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				if(curgara>0){
					curgara--;
					nroGara.setText(String.valueOf(curgara+1));
					loadGaraIntoGui(cal.getAtPosition(curgara));
				}
			}
		});

		lastGara.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				curgara = sizeCal-1;
				nroGara.setText(String.valueOf(curgara+1));
				loadGaraIntoGui(cal.getAtPosition(curgara));
			}
		});

		firstGara.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				curgara = 0;
				nroGara.setText(String.valueOf(curgara+1));
				loadGaraIntoGui(cal.getAtPosition(curgara));
			}
		});

	}

	private void loadGaraIntoGui(final Gara g){
		spinnerDataInizio.setValue(g.getData_inizio().getTime());
		spinnerDurataGiorni.setValue(g.getNro_giorni_da_disputare());
		spinnerDataFine.setValue(g.getData_fine().getTime());

		spinnerDurataGiorni.addChangeListener(new ChangeListener() {

			@Override
			public void stateChanged(ChangeEvent arg0) {
				// TODO Auto-generated method stub
				try{
					System.out.println(spinnerDurataGiorni.getValue());
					int n = Integer.parseInt((String) spinnerDurataGiorni.getValue());
					g.setNro_giorni_da_disputare(n);
					spinnerDataFine.setValue(g.getData_fine());
				}catch(Exception e){
					System.out.println("valore errato");
				}
			}
		});

		nroSfidanti.setText(String.valueOf(g.getGiocatori().size()));
		JPanel p = new JPanel(new FlowLayout(FlowLayout.CENTER));
		for(GiocatoreInterface gio: g.getGiocatori()){
			if(gio instanceof Giocatore){
				p.add(new GiocatoreToggleButton(((Giocatore)gio), false, true));
			}
		}
		scrollSfidanti.setViewportView(p);
		repaint();
		revalidate();
	}

	private void initComponents() {

		jPanel2 = new javax.swing.JPanel();
		firstGara = new javax.swing.JButton();
		previousGara = new javax.swing.JButton();
		nextGara = new javax.swing.JButton();
		lastGara = new javax.swing.JButton();
		javax.swing.JLabel jLabel6 = new javax.swing.JLabel();
		totaleGare = new javax.swing.JTextField();
		jPanel1 = new javax.swing.JPanel();
		javax.swing.JLabel jLabel7 = new javax.swing.JLabel();
		javax.swing.JLabel jLabel8 = new javax.swing.JLabel();
		javax.swing.JLabel jLabel9 = new javax.swing.JLabel();
		spinnerDataInizio = new javax.swing.JSpinner();
		spinnerDataFine = new javax.swing.JSpinner();
		spinnerDurataGiorni = new javax.swing.JSpinner();
		javax.swing.JLabel jLabel11 = new javax.swing.JLabel();
		nroGara = new javax.swing.JTextField();
		jSeparator1 = new javax.swing.JSeparator();
		jSeparator2 = new javax.swing.JSeparator();
		jPanel3 = new javax.swing.JPanel();
		javax.swing.JLabel jLabel10 = new javax.swing.JLabel();
		nroSfidanti = new javax.swing.JTextField();
		scrollSfidanti = new javax.swing.JScrollPane();

		firstGara.setFont(new java.awt.Font("Pericles Light", 1, 12)); // NOI18N
		firstGara.setText("<<");
		jPanel2.add(firstGara);

		previousGara.setFont(new java.awt.Font("Pericles Light", 1, 12)); // NOI18N
		previousGara.setText("<");
		jPanel2.add(previousGara);

		nextGara.setFont(new java.awt.Font("Pericles Light", 1, 12)); // NOI18N
		nextGara.setText(">");
		jPanel2.add(nextGara);

		lastGara.setFont(new java.awt.Font("Pericles Light", 1, 12)); // NOI18N
		lastGara.setText(">>");
		jPanel2.add(lastGara);

		jLabel6.setFont(new java.awt.Font("Pericles Light", 1, 12)); // NOI18N
		jLabel6.setText("Totale gare");
		jPanel2.add(jLabel6);

		totaleGare.setEditable(false);
		totaleGare.setColumns(4);
		totaleGare.setFont(new java.awt.Font("Pericles Light", 1, 12)); // NOI18N
		jPanel2.add(totaleGare);

		jLabel7.setFont(new java.awt.Font("Pericles Light", 1, 12)); // NOI18N
		jLabel7.setText("Data inizio");

		jLabel8.setFont(new java.awt.Font("Pericles Light", 1, 12)); // NOI18N
		jLabel8.setText("Data fine");

		jLabel9.setFont(new java.awt.Font("Pericles Light", 1, 12)); // NOI18N
		jLabel9.setText("Durata in giorni");

		spinnerDataInizio.setFont(new java.awt.Font("Pericles Light", 1, 12)); // NOI18N
		spinnerDataInizio.setModel(new javax.swing.SpinnerDateModel(new java.util.Date(), new java.util.Date(), null, java.util.Calendar.DAY_OF_MONTH));

		spinnerDataFine.setFont(new java.awt.Font("Pericles Light", 1, 12)); // NOI18N
		spinnerDataFine.setModel(new javax.swing.SpinnerDateModel());
		spinnerDataFine.setEnabled(false);

		spinnerDurataGiorni.setFont(new java.awt.Font("Pericles Light", 1, 12)); // NOI18N
		spinnerDurataGiorni.setModel(new javax.swing.SpinnerNumberModel(1, 1, 3, 1));

		jLabel11.setFont(new java.awt.Font("Pericles Light", 1, 12)); // NOI18N
		jLabel11.setText("Gara nro");

		nroGara.setEditable(false);
		nroGara.setColumns(4);
		nroGara.setFont(new java.awt.Font("Pericles Light", 1, 12)); // NOI18N

		jLabel10.setFont(new java.awt.Font("Pericles Light", 1, 12)); // NOI18N
		jLabel10.setText("Totale sfidanti");
		jPanel3.add(jLabel10);

		nroSfidanti.setEditable(false);
		nroSfidanti.setColumns(4);
		nroSfidanti.setFont(new java.awt.Font("Pericles Light", 1, 12)); // NOI18N
		jPanel3.add(nroSfidanti);

		javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
		jPanel1.setLayout(jPanel1Layout);
		jPanel1Layout.setHorizontalGroup(
				jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(jPanel1Layout.createSequentialGroup()
						.addContainerGap()
						.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
								.addComponent(jSeparator2)
								.addComponent(jSeparator1, javax.swing.GroupLayout.Alignment.LEADING)
								.addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
										.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
												.addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
														.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
																.addComponent(jLabel7, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
																.addComponent(jLabel8, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
																.addComponent(jLabel9, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
																.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
																		.addComponent(spinnerDataFine, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 141, javax.swing.GroupLayout.PREFERRED_SIZE)
																		.addComponent(spinnerDataInizio, javax.swing.GroupLayout.PREFERRED_SIZE, 141, javax.swing.GroupLayout.PREFERRED_SIZE)
																		.addComponent(spinnerDurataGiorni, javax.swing.GroupLayout.PREFERRED_SIZE, 141, javax.swing.GroupLayout.PREFERRED_SIZE)))
																		.addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
																				.addComponent(jLabel11)
																				.addGap(5, 5, 5)
																				.addComponent(nroGara, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
																				.addGap(0, 30, Short.MAX_VALUE)))
																				.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																				.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
																						.addComponent(jPanel3, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 414, javax.swing.GroupLayout.PREFERRED_SIZE)
																						.addComponent(scrollSfidanti, javax.swing.GroupLayout.Alignment.TRAILING))
																						.addContainerGap())
				);
		jPanel1Layout.setVerticalGroup(
				jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(jPanel1Layout.createSequentialGroup()
						.addContainerGap()
						.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
								.addGroup(jPanel1Layout.createSequentialGroup()
										.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
												.addGroup(jPanel1Layout.createSequentialGroup()
														.addGap(3, 3, 3)
														.addComponent(jLabel11))
														.addComponent(nroGara, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
														.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
														.addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
														.addGap(5, 5, 5)
														.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
																.addComponent(spinnerDataInizio)
																.addComponent(jLabel7, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
																.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
																		.addComponent(jLabel9)
																		.addComponent(spinnerDurataGiorni, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
																		.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																		.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
																				.addComponent(jLabel8)
																				.addComponent(spinnerDataFine, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
																				.addGroup(jPanel1Layout.createSequentialGroup()
																						.addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
																						.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																						.addComponent(scrollSfidanti, javax.swing.GroupLayout.DEFAULT_SIZE, 91, Short.MAX_VALUE)))
																						.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
																						.addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
																						.addContainerGap())
				);

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
		this.setLayout(layout);
		layout.setHorizontalGroup(
				layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(layout.createSequentialGroup()
						.addContainerGap()
						.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
								.addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
								.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
								.addContainerGap())
				);
		layout.setVerticalGroup(
				layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(layout.createSequentialGroup()
						.addContainerGap()
						.addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
						.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
						.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
						.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
				);
	}

	private javax.swing.JButton firstGara;
	private javax.swing.JPanel jPanel1;
	private javax.swing.JPanel jPanel2;
	private javax.swing.JPanel jPanel3;
	private javax.swing.JSeparator jSeparator1;
	private javax.swing.JSeparator jSeparator2;
	private javax.swing.JButton lastGara;
	private javax.swing.JButton nextGara;
	private javax.swing.JTextField nroGara;
	private javax.swing.JTextField nroSfidanti;
	private javax.swing.JButton previousGara;
	private javax.swing.JScrollPane scrollSfidanti;
	private javax.swing.JSpinner spinnerDataFine;
	private javax.swing.JSpinner spinnerDataInizio;
	private javax.swing.JSpinner spinnerDurataGiorni;
	private javax.swing.JTextField totaleGare;
}
